package com.ssh.api.action;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh.common.log.LogFactory;
import com.ssh.common.log.LogName;
import com.ssh.common.util.JsonResult;
import com.ssh.entity.ChangGui;
import com.ssh.entity.HelpSale;
import com.ssh.manager.ChangGuiManager;
@Controller
@ParentPackage("client")
@Scope("prototype")
@Results(value={@Result(name ="tongjiByTotal",type="json",params={"root","jr","excludeNullProperties","true"}),
		@Result(name ="tongjiByProduct",type="json",params={"root","jr","excludeNullProperties","true"}),
		@Result(name ="BrandCompanySuperMarket",type="json",params={"root","jr","excludeNullProperties","true"}),
		@Result(name ="tongji",type="json",params={"root","jr"})

})

public class TongjiClientAction extends ActionSupport {
	private static final long serialVersionUID = 970773022382019911L;
	JsonResult jr=new JsonResult();
	int dateType;
	int Sort;
	private int brandId;
	private int companyId;
	private int superMarketId;
	private long startDate;
	private long endDate;
	


	@Resource(name="changGuiManagerImpl")
	private ChangGuiManager changGuiManagerImpl;
	
	public  String tongjiByTotal(){
		jr.setMethod("tongjiByTotal");
		Calendar calendar=Calendar.getInstance();
		calendar.setTimeInMillis(System.currentTimeMillis());
		try{
	    	jr.setCode("0");
	    	
	    	switch(dateType){
	    	 case 0:{//当周
	    	 calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH)-7);
	    	 break;
	    	 }
	    	 case 1:{//当月
	    	 calendar.set(Calendar.DAY_OF_MONTH, 1);
	    	 break;
	    	 }
	    	 case 2:{//当季度
	    		 //System.out.println("当前月:"+calendar.get(Calendar.MONTH));
		    	 calendar.set(Calendar.MONTH, 3*((calendar.get(Calendar.MONTH))/3));
		         calendar.set(Calendar.DAY_OF_MONTH,1);  
		    	 break;
	    	 }
	    	 case 3:{//当年
	    	 calendar.set(Calendar.MONTH, 0);
	    	 calendar.set(Calendar.DAY_OF_MONTH, 1);
	    	 break;
	    	 }
	    	}
	    	calendar.set(Calendar.HOUR_OF_DAY, 0);
	    	calendar.set(Calendar.MINUTE, 0);
	    	calendar.set(Calendar.SECOND, 0);
	    	
	    	String groupby="product.id";
	    	switch (Sort) {
			case 1:
				groupby="product.brand.id";
				break;
			case 2:
				groupby="product.superMarket.id";
				break;
			default:
				break;
			}
	    	List<Object> listresult=changGuiManagerImpl.tongjiByTotal(new Timestamp(calendar.getTimeInMillis()),groupby);
	    	jr.setData(listresult);
		}catch (Exception e) {
			jr.setCode("-1");
			jr.setMessage(e.getMessage());
			LogFactory.getDefaultLog().error("查询失败", LogName.API);
		}
		return "tongjiByTotal";
    }
	public  String tongjiByProduct(){
		jr.setMethod("tongjiByProduct");
		Calendar calendar=Calendar.getInstance();
		calendar.setTimeInMillis(System.currentTimeMillis());
		try{
	    	jr.setCode("0");
	    	
	    	switch(dateType){
	    	 case 0:{//当周
	    	 calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH)-7);
	    	 break;
	    	 }
	    	 case 1:{//当月
	    	 calendar.set(Calendar.DAY_OF_MONTH, 1);
	    	 break;
	    	 }
	    	 case 2:{//当季度
	    		 //System.out.println("当前月:"+calendar.get(Calendar.MONTH));
		    	 calendar.set(Calendar.MONTH, 3*((calendar.get(Calendar.MONTH))/3));
		         calendar.set(Calendar.DAY_OF_MONTH,1);  
		    	 break;
	    	 }
	    	 case 3:{//当年
	    	 calendar.set(Calendar.MONTH, 0);
	    	 calendar.set(Calendar.DAY_OF_MONTH, 1);
	    	 break;
	    	 }
	    	}
	    	calendar.set(Calendar.HOUR_OF_DAY, 0);
	    	calendar.set(Calendar.MINUTE, 0);
	    	calendar.set(Calendar.SECOND, 0);
	    	System.out.println(new Date(calendar.getTimeInMillis())+",brandId="+brandId+",companyId="+companyId+",superMarketId="+superMarketId);
	    	List<Object> listresult=changGuiManagerImpl.tongjiByProduct(new Timestamp(calendar.getTimeInMillis()),brandId,companyId,superMarketId);
	    	jr.setData(listresult);
		}catch (Exception e) {
			jr.setCode("-1");
			jr.setMessage(e.getMessage());
			LogFactory.getDefaultLog().error("查询失败", LogName.API);
		}
		return "tongjiByProduct";
    }
	
	public  String find_BrandCompanySuperMarket(){
		
		jr.setMethod("BrandCompanySuperMarket");
		jr.setCode("0");
		try{
			Object listresult=changGuiManagerImpl.find_BrandCompanySuperMarket();
	    	jr.setData(listresult);
		}catch (Exception e) {
			jr.setCode("-1");
			jr.setMessage(e.getMessage());
			LogFactory.getDefaultLog().error("查询失败", LogName.API);
		}
		return "BrandCompanySuperMarket";
	}
	
	public String tongji(){
		jr.setMethod("tongji");
		jr.setCode("0");
		try{
			List<ChangGui> listChangGui=changGuiManagerImpl.listForChangGui(new Timestamp(startDate), new Timestamp(endDate), superMarketId,companyId);
			List<HelpSale> listHelpSale=changGuiManagerImpl.listForHelpSale(new Timestamp(startDate), new Timestamp(endDate), superMarketId,companyId);
			if (listChangGui.size()==listHelpSale.size()) {
				List<Map<String,String>> list=new ArrayList<Map<String,String>>();
				for(int i=0;i<listChangGui.size();i++){
					ChangGui changGui=listChangGui.get(i);
					Map<String,String> map=new HashMap<String,String>();
					map.put("id", changGui.getTaskProductEmployee().getSuperMarketProduct().getProduct().getId().toString());
					map.put("productName", changGui.getTaskProductEmployee().getSuperMarketProduct().getProduct().getName());
					map.put("storecode", changGui.getTaskProductEmployee().getSuperMarketProduct().getStorecode());
					map.put("purchasePrice", changGui.getTaskProductEmployee().getSuperMarketProduct().getPurchasePrice());
					map.put("planPrice", changGui.getTaskProductEmployee().getSuperMarketProduct().getPrice());
					map.put("realPrice", changGui.getPrice());
					map.put("planFenxiao", changGui.getTaskProductEmployee().getFenxiao());
					map.put("fenxiao", changGui.getFenxiao());
					map.put("planStock", changGui.getTaskProductEmployee().getPlanStock()+"");
					map.put("stock", changGui.getStock()+"");
					map.put("planChenlie", changGui.getTaskProductEmployee().getChenlie());
					map.put("chenlie", changGui.getChenlie());
					map.put("barcode", changGui.getTaskProductEmployee().getSuperMarketProduct().getProduct().getBarcode());
					map.put("planDuitou",changGui.getTaskProductEmployee().getDuitou());
					for(int j=0;j<listHelpSale.size();j++){
						long id=listHelpSale.get(j).getTaskProductEmployee().getSuperMarketProduct().getProduct().getId();
					    if (id==Long.parseLong(map.get("id").toString())) {
					    	map.put("duitou", listHelpSale.get(j).getDuitou());
					    	break;
						}  
					}
										
					list.add(map);
				}
				jr.setData(list);
			}
		}catch (Exception e) {
			jr.setCode("-1");
			jr.setMessage(e.getMessage());
			LogFactory.getDefaultLog().error("查询失败", LogName.API);
		}
		return "tongji";	
	}

	public void setJr(JsonResult jr) {
		this.jr = jr;
	}

	public JsonResult getJr() {
		return jr;
	}

	public int getDateType() {
		return dateType;
	}

	public void setDateType(int dateType) {
		this.dateType = dateType;
	}

	public int getSort() {
		return Sort;
	}

	public void setSort(int sort) {
		Sort = sort;
	}

	
	public int getBrandId() {
		return brandId;
	}
	public void setBrandId(int brandId) {
		this.brandId = brandId;
	}
	public int getCompanyId() {
		return companyId;
	}
	public void setCompanyId(int companyId) {
		this.companyId = companyId;
	}
	public int getSuperMarketId() {
		return superMarketId;
	}
	public void setSuperMarketId(int superMarketId) {
		this.superMarketId = superMarketId;
	}
	public long getStartDate() {
		return startDate;
	}
	public void setStartDate(long startDate) {
		this.startDate = startDate;
	}
	public long getEndDate() {
		return endDate;
	}
	public void setEndDate(long endDate) {
		this.endDate = endDate;
	}

	
}
